﻿import QtQuick 2.0
import "../components"
import QtCharts 2.3
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14

Rectangle {
    width: parent.width
    height: parent.height
    radius: 12
    property int btflag: 1
    property int beginx: 55*dp
    property var alldata: CExeConfig.getdeff(btflag)
    property var gzdata: CMaintenance.getFaultInformation("2")
    property int tabvis: 0
    property bool zsqvis: false
    function clearCanvas() {
        var ctx = pinid.getContext('2d');
        ctx.clearRect(0,0,pinid.width,pinid.height);
        pinid.requestPaint();
    }
    onVisibleChanged: {
        CExeConfig.delnulldeff()
        alldata= CExeConfig.getdeff(btflag)
        gzdata=CMaintenance.getFaultInformation("2")
    }

    Toast{
        id: toast3
        y: 200*dp
        x: parent.width / 2 - 150*dp
        z: 1
    }

    Connections{
        target: CExeConfig
        onDefrename:{
            toast3.show("名称不可重复！",1)
        }
    }
    BoxWarning{
        id: defdef
        bt1vis: false
        box_text: "请将其他类型设置为默认状态！"
    }
    BoxWarning{
        id: defdata
        bt1vis: false
        box_text: "默认数据不可删除！"
    }
    BoxWarning{
        id: gzadd
        box_text: "故障信息不允许删除，请确认是否添加！"
        onAccept: {
            CMaintenance.addFaultInformation("2")
            gzdata=CMaintenance.getFaultInformation("2")
        }
    }
    Rectangle{
        x:0*dp
        width: parent.width
        y:50*dp
        height: 1*dp
        color: "#E6E6E6"
    }

    CustomTabButton{
        model: ["默认设置", "国际化","故障"]
        buttonW: 120*dp
        defaultMode1: true
        x:parent.width/2-180*dp
        y:10*dp
        height: 40*dp
        onTextChanged: {
            CExeConfig.delnulldeff()
            if(text==="默认设置"){
                tabvis=0
            }else if(text==="国际化"){
                tabvis=1
            }else if(text==="故障"){
                tabvis=2
            }
        }
    }

    Rectangle{
        id: layout_list
        x:parent.width*0.15
        y:80*dp
        width: parent.width*0.7
        height: parent.height*0.7
        visible: tabvis===0
        onWidthChanged: {
            pinid.requestPaint()
        }

        RowLayout{
            spacing: 10*dp
            CustomButton{
                id:bt1
                width: 150*dp
                height: 46*dp
                title: "注射器类型"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: btflag===1
                defaultMode2:true
                onReleased: {
                    CExeConfig.delnulldeff()
                    zsqvis=false
                    btflag=1
//                    settype(2)
//                    bt2.settype(1)
//                    bt3.settype(1)
                    beginx=50*dp
                    clearCanvas()
                    alldata= CExeConfig.getdeff(btflag)
                }
            }PushButton{
                id:bt2
                width: 150*dp
                height: 46*dp
                title: "盖类型"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: btflag===2
                defaultMode2:true
                onReleased: {
                    CExeConfig.delnulldeff()
                    zsqvis=false
                    btflag=2
//                    settype(2)
//                    bt1.settype(1)
//                    bt3.settype(1)
                    beginx=175*dp
                    clearCanvas()
                    alldata= CExeConfig.getdeff(btflag)
                }
            }CustomButton{
                id:bt3
                width: 150*dp
                height: 46*dp
                title: "耗材名称"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: btflag===3
                defaultMode2:true
                onReleased: {
                    CExeConfig.delnulldeff()
                    zsqvis=true
                    btflag=3
//                    settype(2)
//                    bt1.settype(1)
//                    bt2.settype(1)
                    beginx=300*dp
                    clearCanvas()
                    alldata= CExeConfig.getdeff(btflag)
                }
            }
        }
        Canvas{
            id:pinid
            width: parent.width
            height: 75*dp
            x:0*dp
            y:30*dp
            onPaint: {
                var ctx = getContext("2d");
                draw(ctx);
            }
            function draw(ctx ) {
                // 画之前清空画布
                ctx.clearRect(0, 0, parent.width, parent.height);
                ctx.fillStyle ="#111";           // 设置画笔属性
                ctx.lineWidth = 1.2*dp
                ctx.strokeStyle = "#B8B7B5"
                ctx.beginPath();
                var px = 0;
                var py = 0;
                ctx.beginPath()
                ctx.moveTo(0, 45*dp)
                ctx.lineTo(beginx, 45*dp)
                ctx.stroke()
                ctx.moveTo(beginx, 45*dp)
                ctx.lineTo(beginx+10*dp, 35*dp)
                ctx.stroke()
                ctx.moveTo(beginx+10*dp, 35*dp)
                ctx.lineTo(beginx+20*dp, 45*dp)
                ctx.stroke()
                ctx.moveTo(beginx+20*dp, 45*dp)
                ctx.lineTo(layout_list.width, 45*dp)
                ctx.stroke()

            }
        }
        Text {
            x:20*dp
            y:90*dp
            text: qsTr("名称")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:(layout_list.width-238*dp+width+50*dp)/2
            y:90*dp
            visible: zsqvis
            text: qsTr("注射器数量")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:layout_list.width - 230*dp; y:90*dp
            text: qsTr("是否默认")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:layout_list.width - 95*dp;  y:90*dp
            text: qsTr("操作")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Rectangle{
            x:0*dp
            y:118*dp
            height: 2*dp
            width: defmod.width
            color: "#DEDEDE"
        }



        ScrollView {
            x:0*dp
            y:120*dp
            width: parent.width
            height: parent.height-40*dp
            clip: true
            Column{
                id: myColumnLayout
                width: parent.width
                height: parent.height
                spacing: 1*dp
                Repeater{
                    id:defmod
                    model: alldata
                    delegate: Item {
                        x:0*dp
                        width: layout_list.width
                        height: 60*dp
                        DefTabCOM{
                            width: parent.width
                            height: parent.height
                            defname: modelData.name
                            isdef: modelData.isdef==="1"
                            number: modelData.num
                            defnamev: modelData.namev
                            visb: zsqvis
                            typeid:btflag
                            onBt2: {
                                CExeConfig.adddeff(btflag)
                                alldata=CExeConfig.getdeff(btflag)
                            }
                            onBt3: {
                                if(isdef){
                                    defdata.show()
                                }else{
                                    CExeConfig.deldeff(btflag,number)
                                    alldata=CExeConfig.getdeff(btflag)
                                }
                            }
                            onBt1: {
                                if(isdef){
                                    defdef.show()
                                    isdef=true
                                    alldata=CExeConfig.getdeff(btflag)
                                }else{
                                    CExeConfig.defset(typeid,number)
                                    alldata=CExeConfig.getdeff(btflag)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    Rectangle{
        x:parent.width*0.1
        y:80*dp
        width: parent.width*0.8
        height: parent.height*0.7*dp
        visible: tabvis===1
        color: "white"
        CustomComboBox{
            width: 320*dp
            x:parent.width/2-160*dp
            height: 40*dp
            y:50*dp
            model: ["中文"]
            title: "国际化"
            titleWidth: 60*dp
            editWidth: 260*dp
            onCurrentText: {
                if(text==="English"){
                    //用户使用ComboBox后根据index调用cpp中的方法
                    qmlLanguage.setLanguage(0)
                }else{
                    qmlLanguage.setLanguage(1)
                }
            }
        }
        //        PushButton{
        //            width: 120*dp
        //            height: 40*dp
        //            x:parent.width/2-130*dp
        //            y:parent.height*0.8*dp
        //            title: "取消"
        //            defaultMode2: true
        //        }
        //        PushButton{
        //            width: 120*dp
        //            height: 40*dp
        //            x:parent.width/2+10*dp
        //            y:parent.height*0.8*dp
        //            title: "确定"
        //            defaultMode1: true
        //        }
    }

    // 故障
    Rectangle{
        id: layout_failure
        x:parent.width*0.1
        y:80*dp
        width: parent.width*0.8
        height: parent.height*0.7
        visible: tabvis===2
        color: "white"
        CustomButton{
            title: "新建"
            y:20*dp
            x:parent.width/2+300*dp
            defaultMode1: true
            onReleased: {
                gzadd.show()
            }
        }

        Rectangle{
            x:0*dp
            y:85*dp
            height: 2*dp
            width: defmod2.width
            color: "#DEDEDE"
        }

        Row{
            y: 90*dp
            width: parent.width; height: 38*dp
            Text {
                width:parent.width*0.1
                horizontalAlignment: Text.AlignHCenter
                text: qsTr("序号")
                color: "#666666"
                font.pixelSize: config.fontsize
            }
            Text {
                width:parent.width*0.1
                horizontalAlignment: Text.AlignHCenter
                text: qsTr("故障代码")
                color: "#666666"
                font.pixelSize: config.fontsize
            }
            Text {
                width:parent.width*0.4
                horizontalAlignment: Text.AlignHCenter
                text: qsTr("故障信息")
                color: "#666666"
                font.pixelSize: config.fontsize
            }
            Text {
                width:parent.width*0.2
                horizontalAlignment: Text.AlignHCenter
                text: qsTr("处理方案(是否继续)")
                color: "#666666"
                font.pixelSize: config.fontsize
            }
            Text {
                width:parent.width*0.2
                horizontalAlignment: Text.AlignHCenter
                text: qsTr("是否屏蔽")
                color: "#666666"
                font.pixelSize: config.fontsize
            }
        }
        Rectangle{
            x:0*dp
            y:125*dp
            height: 2*dp
            width: defmod2.width
            color: "#DEDEDE"
        }
        ScrollView {
            x:0*dp
            y:130*dp
            width: parent.width
            height: parent.height-40*dp
            clip: true
            Column{
                id: myColumnLayout2
                width: layout_failure.width
//                height: layout_failure.height
                spacing: 1*dp
                Repeater{
                    id:defmod2
                    model: gzdata
                    delegate: Item {
                        x:0*dp
                        width: layout_failure.width
                        height: 60*dp
                        DefTab2COM{
                            width: parent.width
                            height: parent.height
                            xh:modelData.serial
                            gzdm:modelData.errorCode
                            gzxx:modelData.errorName
                            isjxv:modelData.errorMethod==="是"
                            ispbv:modelData.isBlocked==="是"
                            onRefre: {
                                gzdata=CMaintenance.getFaultInformation("2")
                            }
                        }
                    }
                }
            }
        }

    }
}
